var app = getApp();
Page({
    data: {
        isIPX: app.globalData.isIPX,
        siteroot: app.siteInfo.siteroot.replace('app/index.php', ''),
        uniacid: app.siteInfo.uniacid,
        curPage: "/zofui_sales/order/orderlist",
        page: {
            isend: false,
            doing: false,
            doo: 'order',
            pdata: {
                op: 'list',
                page: 1,
                status: 0,
                ovision: 1,
            },
            waitf: 0,
            nodataf: 0,
        },
        datalist: [],
        tabMenu: [{
            name: "全部",
            status: 0
        }, {
            name: "待付款",
            status: 1
        }, {
            name: "已付款",
            status: 2
        }, {
            name: "待完成",
            status: 3
        }, {
            name: "待评价",
            status: 4
        }, {
            name: "已失效",
            status: 5,
            ishide: 0,
        }],
        reason: false,
        showrrefund: false,
        params: {},
        isshowcode: false,
        showqr: '',
        showcode: '',
        datearr: {},
        actindex: '',
        isshowqrcode: false,
        hxqrcode: '',
    },
    onLoad: function (options) {
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
        var shopid = options.shopid ? options.shopid : '';
        var dealid = options.dealid ? options.dealid : '';
        this.data.options = options;

        self.setData({
            'page.pdata.status': options.status ? options.status * 1 : 0,
            'page.pdata.zfid': zfid,
            'page.pdata.dealid': dealid,
        });

        app.com.auth(function (user) {
            app.com.getPage(self, self.data.page, 0, function (res) {
                var data = res.data.data;
                self.setData({
                    datalist: data.list,
                    params: res.data.data.params,
                    sharedata: res.data.data.sharedata,
                    'tabMenu[2].name': res.data.data.params.payedn,
                    'tabMenu[5].ishide': res.data.data.params.showfail == 1 ? 1 : 0,
                    reason: res.data.data.reason ? res.data.data.reason : false,
                    inited: true,
                });
                if (res.data.data.showchb == 1) {
                    self.selectComponent("#paychb").showhb(res.data.data.showchboid, res.data.data.showchbfee);
                }

            }, false, function (res) {
                app.com.alert(res.data.message);
            });
            app.com.setBar(app, self, shopid);
        });
        wx.setNavigationBarTitle({
            title: '订单',
        });
    },
    onShareAppMessage: function () {
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl: self.data.sharedata.img,
        }
    },
    tabChange(e) {
        var self = this;
        if (self.data.ising || (e.detail.name < 3 && e.detail.name == self.data.page.pdata.status)) return false;
        self.data.ising = true;
        self.setData({
            'page.pdata.status': e.detail.name,
            'page.pdata.page': 1,
            'page.isend': false,
            'page.waitf': 0,
            'page.nodataf': 0
        });

        app.com.getPage(self, self.data.page, 0, function (res) {
            self.setData({
                datalist: res.data.data.list,
            });
        });

        self.data.ising = false;
    },
    onReachBottom: function () {
        var self = this;
        app.com.getPage(self, self.data.page, 0, function (res) {
            self.setData({
                datalist: self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    onPullDownRefresh: function () {
        if (this.data.isdown) return false;
        this.setData({
            isdown: true,
            datalist: [],
            'page.pdata.page': 1,
            'page.isend': false,
            'page.waitf': 0,
            'page.nodataf': 0,
        });
        this.onLoad(this.data.options);
        this.setData({
            isdown: false,
        });
        wx.stopPullDownRefresh();
    },
    getedchb() {

    },
    getRefund(e) {
        var self = this;
        var type = e.currentTarget.dataset.type;
        var i = e.currentTarget.dataset.i;
        var ii = e.currentTarget.dataset.ii;

        this.setData({
            taroid: e.currentTarget.dataset.id,
            showrrefund: true,
            rtype: type,
            ri: i,
            rii: ii
        })
    },
    refundit(e) {
        var self = this;
        var postdata = {
            op: 'applyback',
            oid: this.data.taroid,
            reason: e.detail.name,
        };
        app.com.confirm('确定要申请退款吗？', function () {
            app.com.http('order', 'POST', postdata, 0, true, '', '', function (res) {
                if (!res.data.errno && res.data) {
                    app.com.toast(res.data.message);

                    self.setData({
                        ['datalist[' + self.data.ri + '].isback']: 1,
                    })
                    if (self.data.rtype * 1 == 1) {
                        self.setData({
                            ['datalist[' + self.data.ri + '].down[' + self.data.rii + '].isback']: 1,
                        })
                    } else if (self.data.datalist[self.data.ri].down) {
                        for (var i = 0; i < self.data.datalist[self.data.ri].down.length; i++) {
                            self.setData({
                                ['datalist[' + self.data.ri + '].down[' + i + '].isback']: 1,
                            })
                        }
                    }

                } else {
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    cancelref(e) {
        this.setData({
            showrrefund: false,
        })
    },
    payit(e) {
        var self = this;
        var i = e.currentTarget.dataset.i;
        var oid = e.currentTarget.dataset.oid;

        var postdata = {
            oid: oid,
            op: 'pay',
        }
        app.com.http('order', 'POST', postdata, 0, true, '', '', function (res) {
            if (!res.data.errno && res.data.errno !== undefined) {
                wx.requestPayment({
                    'appId': res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    success: function (payres) {
                        if (payres.errMsg == 'requestPayment:ok') {
                            app.com.toast('支付完成', 'success', function () {
                                self.setData({
                                    ['datalist[' + i + '].status']: 1,
                                })
                            });
                            for (var j = 0; j < self.data.datalist[i].down.length; j++) {
                                self.setData({
                                    ['datalist[' + i + '].down[' + j + '].statusstr']: '已支付',
                                })
                            }
                        }
                    },
                    fail: function (payres) {

                    }
                });
            } else {
                app.com.alert(res.data.message);
            }
        });
    },
    showcode(e) {
        var i = e.currentTarget.dataset.i;
        this.setData({
            isshowcode: true,
            showcode: this.data.datalist[i].code,
            showqr: this.data.datalist[i].hxqrcode,
        })
    },
    hidecode() {
        this.setData({
            isshowcode: false,
        })
    },
    showqrcode(e) {
        var i = e.currentTarget.dataset.i;
        this.setData({
            hxqrcode: this.data.datalist[i].hxqrcode,
            isshowqrcode: true,
        })
    },
    hideqrcode() {
        this.setData({
            isshowqrcode: false,
        })
    },
    yytime(e) {
        var self = this;
        var index = e.currentTarget.dataset.i;

        if (self.data.params.xcxmess && wx.requestSubscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: self.data.params.xcxmess,
                complete: function () {
                    self.yyfb(index);
                }
            })
        } else {
            self.yyfb(index);
        }
    },
    yyfb(index) {
        var self = this;
        self.data.actindex = index;
        var postdata = {
            oid: this.data.datalist[index].id,
            op: 'getyydate',
        }
        app.com.http('order', 'POST', postdata, 0, true, '', '', function (res) {
            if (res.data && !res.data.errno && res.data.errno !== undefined) {
                self.setData({
                    datearr: {
                        actgid: res.data.data.gid,
                        mapid: res.data.data.mapid,
                        date: res.data.data.dateday,
                        time: res.data.data.datetimes,
                        isprice: 0,
                    }
                }, function () {
                    self.selectComponent('#showdate').showthis();
                })
            } else {
                app.com.alert(res.data.message);
            }
        });
    },
    confirmdate(e) {
        var self = this;
        var index = self.data.actindex;
        var postdata = {
            oid: this.data.datalist[index].id,
            op: 'setyydate',
            date: e.detail.date,
            time: e.detail.time ? e.detail.time : '',
        }
        app.com.confirm('确定预约' + postdata.date + ' ' + postdata.time + '吗？', function () {
            app.com.http('order', 'POST', postdata, 0, true, '', '', function (res) {
                if (res.data && !res.data.errno && res.data.errno !== undefined) {
                    app.com.alert(res.data.message);

                    self.setData({
                        ['datalist[' + index + '].isneedyy']: 2,
                    })

                } else {
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    location: function (e) {
        app.com.location(e);
    },
    otherapp: function (e) {
        app.com.otherapp(e);
    },
    navigateto: function (e) {
        app.com.navigateto(e);
    },
    redirectto: function (e) {
        app.com.redirectto(e);
    },
    callphone: function (e) {
        app.com.callphone(e);
    },
    toweburl: function (e) {
        app.com.toweburl(e);
    },
    showimages: function (e) {
        app.com.showimages(e);
    },
    copy(e) {
        app.com.copy(e);
    },
    footNavigate(e) {
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
            url: url
        })
    },
    // 取消订单
    cancelorder(e) {
        var self = this;
        var id = e.currentTarget.dataset.id;
        var postdata = {
            oid: id,
            op: 'cancel'
        };
        app.com.confirm('确定取消订单吗？', function () {
            app.com.http('good_v1', 'POST', postdata, 0, true, '', '', function (res) {
                if (!res.data.errno && res.data.errno !== undefined) {
                    app.com.toast(res.data.message, 'none', function () {
                        // self.setData({
                        //     'order.unset' : 1,
                        // })
                        // wx.navigateBack();
                        self.setData({
                            datalist: [],
                            'page.pdata.page': 1,
                            'page.isend': false,
                            'page.waitf': 0,
                            'page.nodataf': 0,
                        });
                        self.onLoad(self.data.options);
                    });
                } else {
                    app.com.alert(res.data.message);
                }
            });
        });
    },

})